package com.ruoyi.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class KafkaConsumer
{
    private static final Logger log = LoggerFactory.getLogger(KafkaConsumer.class);

    @KafkaListener(topics = KafkaProducerService.TOPIC_TEST, groupId = KafkaProducerService.TOPIC_GROUP1)
    public void topic_test_group1(ConsumerRecord<?,?>record, Acknowledgment ack, String topic)
    {
        Optional message = Optional.ofNullable(record.value());
        if(message.isPresent())
        {
            Object msg = message.get();
            log.info("topic.group1 consumer :  topic:"+topic+", message: "+msg);
            ack.acknowledge();
        }
    }

    @KafkaListener(topics = KafkaProducerService.TOPIC_TEST, groupId = KafkaProducerService.TOPIC_GROUP2)
    public void topic_test_group2(ConsumerRecord<?,?>record, Acknowledgment ack, String topic)
    {
        Optional message = Optional.ofNullable(record.value());
        if(message.isPresent())
        {
            Object msg = message.get();
            log.info("topic.group2 consumer :  topic:"+topic+", message: "+msg);
            ack.acknowledge();
        }
    }
}
